#include "head.h"

//声明函数
//单链表的创建
u_list *user_init_list(void)
{
    u_list *list = (u_list *)malloc(sizeof(u_list));
    if(NULL == list)
    {
        ERRLOG("创建用户单链表失败\n");
        return NULL;
    }
    //头节点指向空
    list->next = NULL;
    return list;
}

//单链表的插入
void user_insert(u_list *list , f_user user ,int pos )
{
    //安全判断
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return ; 
    }

    //创建新节点
    u_node * new_node =(u_node *)malloc(sizeof(u_node));
    if (NULL == new_node)
    {
        ERRLOG("创建动态内存失败\n");
        return ;
    }
    
    //指针初始化、写入数据
    new_node->next = NULL;
    new_node->user = user;
    
    //头插
    if(pos == 1)
    {
        //新节点的next 指向 list的next
        new_node->next = list->next;
        
        //list的next = 新节点
        list->next = new_node;
        
        return;
    }

    //任意插入
    //定义ptr指向需要插入的位置的上一个节点list->next
    u_node *ptr = list->next;

    //遍历搜索
    for (size_t i = 1 ;i< pos - 1 && ptr!= NULL ; i++)
    {
        ptr = ptr->next;
    }
    
    //判断是否越界
    if(NULL == ptr)
    {
        printf("ptr 已指向NULL\n");
        free(new_node);
        return ;
    }

    //新节点的next指向ptr的next
    new_node->next = ptr->next;

    //ptr的next  指向 等于 ptr 
    ptr->next = new_node;

    return ;
}

//单链表的删除
void user_delete_pos(u_list *list ,int pos )
{
        if (NULL == list)
    {
        printf("链表 为 NULL\n");
        return;
    }
    if (NULL == list->next)
    {
        printf("链表 为无数据\n");
        return;
    }

    u_node *ptr_1 = list->next;
    u_node *ptr_2 = NULL;
    if (1 == pos)
    {
        list->next = ptr_1->next;
        free(ptr_1);
        return;
    }

    for (size_t i = 0; ptr_1 != NULL; i++)
    {

        ptr_1 = ptr_1->next;
    }
    if (NULL == ptr_1)
    {
        printf("ptr_1指向null\n");
        return;
    }
    ptr_2 = ptr_1->next;
    ptr_1 = ptr_2->next;

    free(ptr_2);
    return;
}

//单链表的查找
u_node * user_find_pos(u_list *list ,int pos );

//单链表的修改

//单链表的遍历
void printf_user_list(u_list *list)
{
    if(NULL == list)
    {
        ERRLOG("单链表为NULL\n");
        return ;
    }
    if(NULL == list->next)
    {
        ERRLOG("单链表内无数据\n");
        return;
    }

    //定义一个指针指向头节点 
    u_node *ptr = list->next;

    while(ptr != NULL)
    {
        printf("账号:%s 密码:%s 用户身份证信息:%d 余额:%d \n",ptr->user.name,ptr->user.password,ptr->user.IDcard,ptr->user.value);
        ptr = ptr->next;
    }
    
    return ;
}

//单链表的合并
u_list *merge_user_list(u_list *list1, u_list *list2)
{
    if (NULL == list1 || NULL == list2)
    {
        ERRLOG("单链表的合并 : 链表不存在");
        return list1;
    }

    // 1、求链表list1 的链表长度
    u_node *ptr_1 = list1->next;
    int len = 0;
    
    while (NULL != ptr_1)
    {
        ptr_1 = ptr_1->next;
        len++;
    }
    // 插入到链表中
    ptr_1 = list1->next;
    u_node *ptr_2 = list2->next;
    while (ptr_2 != NULL)
    {
        len++;
        user_insert(list1, ptr_2->user, len);
        ptr_2 = ptr_2->next;
    }

    return list1;
}
//单链表的查找
u_node * user_find_name(u_list *list ,char *name )
{
    if (NULL == list)
    {
        ERRLOG("单链表为空\n");
        return NULL;
    }
    if(NULL == list->next)
    {
        ERRLOG("单链表内无数据\n");
        return NULL;
    }

    u_node *ptr = list->next;
        
    while (ptr != NULL ) 
    {
        if (! strcmp(ptr->user.name, name))
        {
            return ptr; 
        }
        ptr = ptr->next;
    }
    
    return NULL;

}

//单链表的销毁
void free_user_list(u_list ** list)
{
    if (NULL == *list)
    {
        printf("单链表为NULL\n");
        return;
    }
    if (NULL == (*list)->next)
    {
        printf("单链表内无数据\n");
        free(*list);
        *list = NULL;
        return ;
    }

    u_node *ptr_1 = (*list)->next;
    u_node *ptr_2 = ptr_1->next;

    while (ptr_1 != NULL)
    {
        free(ptr_1);
        ptr_1 = ptr_2;
        if(ptr_2 !=NULL)
        {
            ptr_2 = ptr_2->next;
        }
    }

    //释放节点
    free(*list);
    *list = NULL;
}


